/**
 * Info.m by Giraldo Rosales.
 * Visit www.liquidgear.net for documentation and updates.
 *
 * Copyright (c) 2009 Nitrogen Design, Inc. All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 **/

#import "Info.h"
#import "LiquidGearAppDelegate.h"
#import "Utils.h"
#import "WebController.h"

@implementation Info

@synthesize viewController;

- (id)initWithControl:(WebController*)controller {
	viewController	= controller;
	return self;
}

/*
 * returns a JS String with various device settings
 *  - ID
 *  - System Platform
 *  - System OS
 *  - Model
 *  - LiquidGear Version
 *  - Applicaiton Version
 */
- (void)getInfo {
	LiquidGearAppDelegate *LGDelegate = (LiquidGearAppDelegate *)[[UIApplication sharedApplication] delegate];
	NSString *appVer	= LG_VERSION_APP;
	NSString *wifi		= @"false";
	UIDevice *device	= [UIDevice currentDevice];
	
	if(kSCNetworkReachabilityFlagsIsWWAN) {
		wifi = @"true";
	}
	
	NSString* bundleVersion	= [NSString stringWithFormat:@"%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]];
	
	//Battery State
	UIDeviceBatteryState state	= [device batteryState];
	NSString *batteryState;
	
	if(state == UIDeviceBatteryStateUnplugged) {
		batteryState = @"unplugged";
	}
	else if(state == UIDeviceBatteryStateCharging) {
		batteryState = @"charging";
	}
	else if(state == UIDeviceBatteryStateCharging) {
		batteryState = @"full";
	} else {
		batteryState = @"unknown";
	}
	
	NSDictionary *callback	= [NSDictionary dictionaryWithObjectsAndKeys:
														 [NSString stringWithFormat:@"%s", [appVer UTF8String]], @"appVersion",
														 [NSString stringWithFormat:@"%f", [device batteryLevel]], @"batteryLevel",
														 batteryState, @"batteryState",
														 [NSString stringWithFormat:@"%s", [[device uniqueIdentifier] UTF8String]], @"id",
														 [NSString stringWithFormat:@"%s", [bundleVersion UTF8String]], @"lgVersion",
														 [NSString stringWithFormat:@"%s", [[device model] UTF8String]], @"model",
														 [NSString stringWithFormat:@"%s", [[device name] UTF8String]], @"name",
														 [NSString stringWithFormat:@"%s", [[device systemName] UTF8String]], @"os",
														 [NSString stringWithFormat:@"%d", [device proximityState]], @"proximity",
														 [NSString stringWithFormat:@"%s", [[device systemVersion] UTF8String]], @"version",
														 [NSString stringWithFormat:@"%s", [wifi UTF8String]], @"wifi",
														 nil
														 ];
	
	[[[Utils alloc] autorelease] jsCall:viewController.webView type:@"info" data:callback directCall:nil];
}

- (void)dealloc {
	[super dealloc];
}

@end